package cn.wowjoy.pda.android_pharmacy.mvvm.presenter;

import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import cn.wowjoy.pda.android_pharmacy.api.ApiService;
import cn.wowjoy.pda.android_pharmacy.common.core.BasePresenter;
import cn.wowjoy.pda.android_pharmacy.common.core.http.ServiceFactory;
import cn.wowjoy.pda.android_pharmacy.common.widget.CreateDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MyToast;
import cn.wowjoy.pda.android_pharmacy.model.xml.XmlUtils;
import cn.wowjoy.pda.android_pharmacy.model.xmlBaseResponse.RoleInfoResponseEnvelope;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.inHospitalPatientInfo.InHospitalPatientBeen;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.updatePatientInfo.UpdateSendNode;
import cn.wowjoy.pda.android_pharmacy.mvvm.vm.InHospitalSendDetailViewModel;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/8/16.
 */

public class InHospitalSendDetailPresenter extends BasePresenter<InHospitalSendDetailViewModel> {
    public ArrayList<InHospitalPatientBeen> FitstData =new ArrayList<>();
    private ArrayList<InHospitalPatientBeen> mTestBeen=new ArrayList<>();
    public String DJJLID;
    private ApiService mApiService;
    private MDialog waitDialog;

    @Override
    public void onCreatePresenter() {
        loadData(false);
        mApiService = (ApiService) ServiceFactory.getInstance().getService(ApiService.class);
    }

    @Override
    public void loadData(boolean ref) {
        FitstData.clear();
        List<InHospitalPatientBeen> employeeInfoBeenList = (List<InHospitalPatientBeen>) activity.getIntent().getExtras().getSerializable("inHospitalPatientBeenList");
        DJJLID=activity.getIntent().getExtras().getString("DJJLID");
        if(employeeInfoBeenList != null && employeeInfoBeenList.size() != 0){
            FitstData.addAll(employeeInfoBeenList);
            if(FitstData!=null ){
                viewModel.setWData(FitstData);
            }
        }
    }

    public void sendUpdate(String DJCZLX,String CZYHID){
        Disposable disposable = null;
        if(DJJLID.substring(0,1).equalsIgnoreCase("Z")){
            disposable = mApiService.doXml(XmlUtils.prepare(UpdateSendNode.getContent("PDAU06",DJJLID.substring(1,DJJLID.length()),"12107",DJCZLX,CZYHID,"8")))
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeWith(new DisposableObserver<RoleInfoResponseEnvelope>() {
                        @Override
                        protected void onStart() {
                            if(null == waitDialog){
                                waitDialog = CreateDialog.waitingDialog(activity);
                            }
                        }
                        @Override
                        public void onNext(RoleInfoResponseEnvelope xmlData) {
                            CreateDialog.dismiss(activity);
                            if (!"0".equals(xmlData.body.roleInfoResponse.Etrack_ProcInterfaceResult)) {
                                mOnSubmitResult.submitFail("服务器异常！");
                                return;
                            }
                            mOnSubmitResult.submitSuccess();
                        }
                        @Override
                        public void onComplete() {
                        }
                        @Override
                        public void onError(Throwable e) {
                            CreateDialog.dismiss(activity);
                            if(null != e && null != e.getMessage()){
                                Log.e("Throwable", e.getMessage());
                                new MyToast(activity).showinfo(e.getMessage());
                            }
                        }
                    });
        }else{
            disposable = mApiService.doXml(XmlUtils.prepare(UpdateSendNode.getContent("PDAU06",DJJLID,"12107",DJCZLX,CZYHID,"8")))
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeWith(new DisposableObserver<RoleInfoResponseEnvelope>() {
                        @Override
                        protected void onStart() {
                            if(null == waitDialog){
                                waitDialog = CreateDialog.waitingDialog(activity);
                            }
                        }
                        @Override
                        public void onNext(RoleInfoResponseEnvelope xmlData) {
                            CreateDialog.dismiss(activity);
                            if (!"0".equals(xmlData.body.roleInfoResponse.Etrack_ProcInterfaceResult)) {
                                mOnSubmitResult.submitFail("服务器异常！");
                                return;
                            }
                            mOnSubmitResult.submitSuccess();
                        }
                        @Override
                        public void onComplete() {
                        }
                        @Override
                        public void onError(Throwable e) {
                            CreateDialog.dismiss(activity);
                            if(null != e && null != e.getMessage()){
                                Log.e("Throwable", e.getMessage());
                                new MyToast(activity).showinfo(e.getMessage());
                            }
                        }
                    });
        }

        addDisposable(disposable);
    }
    //药单更新监听
    private OnSubmitHospitalResult mOnSubmitResult;

    public void registeOnSubmitResultListener(OnSubmitHospitalResult mOnScanResult) {
        this.mOnSubmitResult = mOnScanResult;
    }

    public void unRegisteOnSubmitResultListener() {
        this.mOnSubmitResult = null;
    }
    //药单更新监听
    public interface OnSubmitHospitalResult {
        void submitSuccess();
        void submitFail(String errMsg);
    }

}
